package es.wtestgen.action.profesor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

import es.wtestgen.action.WTestGenAction;
import es.wtestgen.bean.profesor.AsignaturaForm;
import es.wtestgen.domain.Asignatura;
import es.wtestgen.domain.Profesor;
import es.wtestgen.services.AsignaturaDAO;
import es.wtestgen.services.WTestGenService;
import es.wtestgen.util.HibernateUtil;

public class MantenimientoAsignaturasAction extends WTestGenAction {
	
	private static final Log log = LogFactory.getLog(MantenimientoAsignaturasAction.class);

	@Override
	public ActionForward _execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)  {

		log.info("_________Estamos en MantenimientoAsignaturasAction, gestiona todos los tramites con las asignaturas");	
		String accion = request.getParameter("accion");
		
		ActionForward forward = null;
		
		if(accion == null || accion.equals("")){
			forward = listar(mapping, form, request, response);
		}else if(accion.equals("nuevo")){
			forward = nuevo(mapping, form, request, response);
		}else if(accion.equals("eliminar")){
			forward = eliminar(mapping, form, request, response);
		}else if(accion.equals("editar")){
			forward = editar(mapping, form, request, response);
		}else if(accion.equals("guardar")){
			forward = guardar(mapping, form, request, response);
		}

		return forward;
	}
	
	
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			 {
		
		AsignaturaForm asignaturaForm = (AsignaturaForm) form;
		Asignatura asignatura = new Asignatura();
		
		request.setAttribute("asignatura", asignatura);
		return mapping.findForward("nuevoAsignatura");
	}

	public ActionForward eliminar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		AsignaturaForm asignaturaForm = (AsignaturaForm) form;
		
		String codAsig = request.getParameter("codAsig");
		
		AsignaturaDAO asignaturaDao = new AsignaturaDAO();
		
		/*
		 * Con esta clase que hereda de ActionForward le podemos añadir parametros a las
		 * redirecciones forward
		 */
		ActionRedirect actionRedirect = null;
		
		if(asignaturaDao.eliminar(Integer.parseInt(codAsig))){
			ActionForward findForward = mapping.findForward("eliminadoAsignatura");
			actionRedirect = new ActionRedirect(findForward);
			actionRedirect.addParameter("accion", "");
			actionRedirect.addParameter("eliminadoOk", "si");
			return actionRedirect;			
		}else{
			return mapping.findForward("errorEliminarAsignatura");
		}
	}
	
	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		AsignaturaForm asignaturaForm = (AsignaturaForm) form;
		
		String codAsig = request.getParameter("codAsig");
		
		AsignaturaDAO asignaturaDao = new AsignaturaDAO();
		
		List<Asignatura> asignaturas = asignaturaDao.findById(codAsig);
		Asignatura asignatura = (Asignatura)asignaturas.get(0);
		
		asignaturaForm.setCursoAsig(asignatura.getCursoAsig());
		asignaturaForm.setNombreAsig(asignatura.getNombreAsig());
		asignaturaForm.setCodAsig(String.valueOf(asignatura.getCodAsig()));
		
		request.setAttribute("asignaturaForm", asignaturaForm);
		return mapping.findForward("nuevoAsignatura");
	}
	
	public ActionForward guardar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Profesor profesor = (Profesor)request.getSession().getAttribute("usuario");
		
		AsignaturaForm asignaturaForm = (AsignaturaForm) form;
				
		AsignaturaDAO asignaturaDao = new AsignaturaDAO();		
		
		Asignatura asignatura = new Asignatura();
		
		asignatura.setNombreAsig(asignaturaForm.getNombreAsig());
		asignatura.setCursoAsig(asignaturaForm.getCursoAsig());
		asignatura.setProfesor(profesor);
		
		if(asignaturaForm.getCodAsig() != null && !asignaturaForm.getCodAsig().equals("") && !asignaturaForm.getCodAsig().equals("0")){
			int id = Integer.parseInt(asignaturaForm.getCodAsig());
			asignatura.setCodAsig(id);
		}else{
			asignatura.setCodAsig(-1);
		}
		
		ActionRedirect actionRedirect = null;
		
		if(asignaturaDao.guardar(asignatura)){
			ActionForward findForward = mapping.findForward("guardadoAsignatura");
			actionRedirect = new ActionRedirect(findForward);
			actionRedirect.addParameter("accion", "");
			actionRedirect.addParameter("guardadoOk", "si");
			return actionRedirect;			
		}else{
			return mapping.findForward("error");
		}
		
	}

	public ActionForward listar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Profesor profesor = (Profesor)request.getSession().getAttribute("usuario");
		
		AsignaturaForm asignaturaForm = (AsignaturaForm)form;
		
		AsignaturaDAO asignaturaDao = new AsignaturaDAO();
		
		List<Asignatura> asignaturas = asignaturaDao.findAsignaturasProfesor(profesor);
		
		if(asignaturas != null && asignaturas.size()>0){
			asignaturaForm.setAsignaturas(asignaturas);
		}else{
			asignaturaForm.setAsignaturas(new ArrayList<Asignatura>());			
		}
		
		request.setAttribute("asignaturas", asignaturaForm.getAsignaturas());			
		return mapping.findForward("listarAsignaturas");
	}
	

}
